<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  content_for :page_title, t(:page_title, "Statistics for %{account}", :account => @account.name)
  @active_tab = "statistics"
  css_bundle :statistics
  js_bundle :account_statistics
%>
<% @customLinks = account_custom_links; if @customLinks %>
  <% content_for :right_side do %>
    <% @customLinks.each do |link| %>
      <a href="<%= link[:url] %>" role="button" class="btn button-sidebar-wide"><i class="<%= link[:icon_class] %>" role="presentation"></i> <%= link[:text] %></a>
    <% end %>
  <% end %>
<% end %>
<h1 class="screenreader-only">{{#t}}Account Statistics{{/t}}</h1>
<% if @counts_report %>
  <h2>{{#t}}General Numbers{{/t}}</h2>
  <table class="numbers_table">
    <tr>
      <th scope="row"><%= before_label(:generated_at, "Generated") %></th>
      <td><%= datetime_string(@counts_report['generated_at']) %></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:courses, "Courses") %></th>
      <td><%= n(@counts_report['courses']) %></td>
      <td style="font-size: 0.8em; padding-left: 20px;"><a href="#" class="over_time_link" data-key="courses" data-name="{{#t}}Courses") %>"><%= t(:over_time_link, "over time{{/t}}</a></td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:teachers, "Teachers") %></th>
      <td><%= n(@counts_report['teachers']) %></td>
      <td style="font-size: 0.8em; padding-left: 20px;"><a href="#" class="over_time_link" data-key="teachers" data-name="{{#t}}Teachers") %>"><%= t(:over_time_link, "over time{{/t}}</a></td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:students, "Students") %></th>
      <td><%= n(@counts_report['students']) %></td>
      <td style="font-size: 0.8em; padding-left: 20px;"><a href="#" class="over_time_link" data-key="students" data-name="{{#t}}Students") %>"><%= t(:over_time_link, "over time{{/t}}</a></td>
    </tr>
    <tr>
      <th scope="row"><%= before_label(:users, "Users") %></th>
      <td><%= n(@counts_report['users']) %></td>
      <td style="font-size: 0.8em; padding-left: 20px;"><a href="#" class="over_time_link" data-key="users" data-name="{{#t}}Users") %>"><%= t(:over_time_link, "over time{{/t}}</a></td>
    </tr>
    <tr>
      <th scope="col">{{#t}}File Storage{{/t}}</th>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <th scope="row">&nbsp;&nbsp;<%= before_label(:uploaded_storage, "Uploaded") %></th>
      <td class='number'>
        <%= t(:uploaded_data, { :one => "%{file_size} *from one file*", :other => "%{file_size} *from %{count} files*" },
             :file_size => number_to_human_size(@counts_report['files_size'] || 0), :count => @counts_report['files'],
             :wrapper => '<span style="font-size: 0.8em;">\1</span>') %>
      </td>
      <td style="font-size: 0.8em; padding-left: 20px;"><a href="#" class="over_time_link" data-key="files_size" data-name="{{#t}}Uploaded File Storage") %>"><%= t(:over_time_link, "over time{{/t}}</a></td>
    </tr>
    <tr>
      <th scope="row">&nbsp;&nbsp;<%= before_label(:media_storage, "Media") %></th>
      <td class='number'>
        <%= t(:media_data, { :one => "%{file_size} *from one file*", :other => "%{file_size} *from %{count} files*" },
             :file_size => number_to_human_size(@counts_report['media_files_size'] || 0), :count => @counts_report['media_files'],
             :wrapper => '<span style="font-size: 0.8em;">\1</span>') %>
      </td>
      <td style="font-size: 0.8em; padding-left: 20px;"><a href="#" class="over_time_link" data-key="media_files_size" data-name="{{#t}}Media File Storage") %>"><%= t(:over_time_link, "over time{{/t}}</a></td>
    </tr>
  </table>
<% end %>

<% if can_do @account, @current_user, :read_course_list %>
<% if @account == Account.default %>
<h2>{{#t}}Recently Created Courses{{/t}}</h2>
<ul id="recently_created_item_list" class="item_list">
  <% @recently_created_courses.each do |course| %>
    <li>
      <a href="<%= course_path(course.id) %>" class="header"><%= course.name %></a>
      <div class="sub_header">
        <% teacher = course.teachers.first %>
        <% if teacher %>
          <%= mt(:created_at_by, "created %{at} by [%{teacher}](%{url})",
              :at => datetime_string(course.created_at),
              :teacher => teacher.name,
              :url => user_url(teacher.id)) %>
        <% else %>
          <%= t(:created_at, "created %{at}",
              :at => datetime_string(course.created_at)) %>
        <% end %>
      </div>
    </li>
  <% end %>
  <% if @recently_started_courses.empty? %>
    <li>{{#t}}None to show{{/t}}</li>
  <% end %>
</ul>
<% end %>

<h2>{{#t}}Recently Started Courses{{/t}}</h2>
<ul id="recently_started_item_list" class="item_list">
  <% @recently_started_courses.each do |course| %>
    <li>
      <a href="<%= course_path(course.id) %>" class="header"><%= course.name %></a>
      <div class="sub_header"><%= t(:started_at, "started %{at}", :at => datetime_string(course.start_at) ) %></div>
    </li>
  <% end %>
  <% if @recently_started_courses.empty? %>
    <li>{{#t}}None to show{{/t}}</li>
  <% end %>
</ul>

<h2>{{#t}}Recently Ended Courses{{/t}}</h2>
<ul id="recently_ended_item_list" class="item_list">
  <% @recently_ended_courses.each do |course| %>
    <li>
      <a href="<%= course_path(course.id) %>" class="header"><%= course.name %></a>
      <div class="sub_header"><%= t(:concluded_at, "concluded %{at}", :at => datetime_string(course.conclude_at) ) %></div>
    </li>
  <% end %>
  <% if @recently_ended_courses.empty? %>
    <li>{{#t}}None to show{{/t}}</li>
  <% end %>
</ul>
<% end %>

<% if can_do @account, @current_user, :read_roster %>
<h2>{{#t}}Recently Logged-In Users{{/t}}</h2>
<ul id="recently_logged_in_item_list" class="item_list">
  <% @recently_logged_users.each do |user| %>
    <li>
      <a href="<%= user_path(user.id) %>" class="header"><%= user.name %></a>
      <div class="sub_header"><%= t(:last_logged_in_at, "last logged in %{at}", :at => datetime_string(user.pseudonyms.map{|p| [p.current_login_at, p.last_login_at]}.flatten.compact.max) ) %></div>
    </li>
  <% end %>
  <% if @recently_logged_users.empty? %>
    <li>{{#t}}None to show{{/t}}</li>
  <% end %>
</ul>
<% end %>

<div id="over_time_dialog" style="display: none;">
  <div id='over_time' style='width: 600px; height: 240px;'></div>
  <div style="text-align: right;">
    <a href="#" class="csv_url xls">{{#t}}Download csv{{/t}}</a>
  </div>
  <a href="<%= context_url(@context, :context_statistics_graph_url, '{{ attribute }}') %>" class="over_time_url" style="display: none;">&nbsp;</a>
</div>

<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script> google.load('visualization', '1', {'packages':['annotatedtimeline']}); </script>